//
//  TopK.swift
//  heap
//
//  Created by lhc on 2024/8/9.
//

import Foundation


class Solution {
    func smallestK(_ arr: [Int], _ k: Int) -> [Int] {
        
        guard arr.count > 0, k > 0, arr.count >= k else {return [Int]()}
        
        let queue = PriorityQueue<Int>()
        
        for i in 0..<k {
            queue.enqueue(e: arr[i])
        }
        
        for i in k..<arr.count {
            guard let max = queue.getFront(),
                  arr[i] < max else {
                continue
            }
            
            let _ = queue.dequeue()
            queue.enqueue(e: arr[i])
        }
        
        var ret = [Int]()
        for _ in 0..<k {
            guard let max = queue.dequeue() else {continue}
            
            ret.append(max)
        }
        
        return ret
    }
}
